<?php

namespace app\beautiful\controller\admin\goods\sku;

use app\BaseController;
use app\beautiful\model\goods\GoodsOtherSkuDetailModel;
use app\beautiful\model\goods\GoodsOtherSkuModel;
use app\beautiful\model\mall\GoodsModel;
use app\common\logic\DataLogic;
use think\facade\Db;


class GoodsOtherSku extends BaseController{
    /**
     * 规格列表
     */
    public function index()

    {
        $search = $this->request->param('search');

        $status = $this->request->param('status');

        $type = $this->request->param('type');

        $origin_type = $this->request->param('origin_type/d',1);

        $skuModel = new GoodsOtherSkuModel;

        $skuModel = $skuModel->with(['otherSkuDetail'=>function($query){

            $query->where(['shop_id'=>$this->request->shopId,'is_delete'=>0]);

        }]);

        $where = [

            ['is_delete','=',0],

            ['shop_id','=',$this->request->shopId],

            ['origin_type','=',$origin_type]

        ];

        if($search){

            $where[]=['group_name','like','%'.$search.'%'];
        }
        if($status){

            $where[]=['status','=',$status];
        }
        if(in_array($type,[1,2,3])){

            $where[]=['type','=',$type];
        }
        $order = ['other_sku_id'=>'desc'];

        $field = "other_sku_id,type,is_checkbox,checkbox_num,group_name,status";

        $return = DataLogic::getDataList($skuModel,$where,$order,$field,$this->request->limit,$this->request->page,[]);


        return $this->success($return);

    }
    /**
     * 获取规格
     */
    public function show()

    {
        $other_sku_id = $this->request->param('other_sku_id/d',0);

        $origin_type = $this->request->param('origin_type/d',1);

        if(null === $sku= GoodsOtherSkuModel::where(['other_sku_id'=>$other_sku_id,'shop_id'=>$this->request->shopId,'is_delete'=>0,'origin_type'=>$origin_type])->find()) return $this->error('规则不存在');

        $sku['group_detail'] = GoodsOtherSkuDetailModel::where([

            ['shop_id','=',$this->request->shopId],

            ['other_sku_id','=',$sku->other_sku_id],

            ['is_delete','=',0]

        ])->select()->toArray();


        return $this->success($sku);

    }
    /**
     * 添加/修改规格
    */
    public function save()

    {
        $other_sku_id = $this->request->param('other_sku_id/d',0);
        $type = $this->request->param('type/d',1);
        $is_checkbox = $this->request->param('is_checkbox/d',1);
        $checkbox_num = $this->request->param('checkbox_num/d',1);
        $is_required = $this->request->param('is_required/d',0);
        $group_name = $this->request->param('group_name');
        $group_detail = $this->request->param('group_detail');
        $status = $this->request->param('status/d',-1);
        $origin_type = $this->request->param('origin_type/d',1);


        //判断传来的参数

        if(empty($group_name)) return $this->error('请输入分组名称');

        if(empty($group_detail)) return $this->error('请输入分组明细');

        //保存操作

        if($other_sku_id != 0){

            if(null === $sku= GoodsOtherSkuModel::where(['other_sku_id'=>$other_sku_id,'shop_id'=>$this->request->shopId,'is_delete'=>0])->find()) return $this->error('规则不存在');

        }else{

            $sku = new GoodsOtherSkuModel();

            $sku->shop_id = $this->request->shopId;

        }

        Db::startTrans();

        try{

            $sku->type=$type;

            $sku->origin_type=$origin_type;

            $sku->is_checkbox=$is_checkbox;

            $sku->checkbox_num=$checkbox_num;

            $sku->is_required=$is_required;

            $sku->group_name=$group_name;

            $sku->status=$status;

            $sku->save();

            if($group_detail){
                $old_group_detail=GoodsOtherSkuDetailModel::where([

                    ['shop_id','=',$this->request->shopId],

                    ['other_sku_id','=',$sku->other_sku_id],

                    ['is_delete','=',0]

                ])->select()->toArray();

                $update_group_detail_ids=array_column($group_detail,'other_sku_detail_id') ?? []; $delete_group_detail=[];

                $old_group_detail_ids=array_column($old_group_detail,'other_sku_detail_id') ?? [];

                foreach ($old_group_detail as $k => $v){

                    if(!in_array($v['other_sku_detail_id'],$update_group_detail_ids)){
                        //是否有被选中
                        $unit = GoodsModel::where(['shop_id'=>$v['shop_id'],'is_delete'=>0,'unit_id'=>$v['other_sku_detail_id']])->find();

                        if($unit) return $this->error('该单位已被选中，不能删除');

                        $v['is_delete']=1;

                        $delete_group_detail[]=$v;

                    }

                }

                foreach ($group_detail as $k => $v){

                    $group_detail[$k]['shop_id'] = $this->request->shopId;

                    $group_detail[$k]['other_sku_id'] = $sku->other_sku_id;

                    $group_detail[$k]['type'] = $sku->type;

                    if(isset($v['other_sku_detail_id'])){

                        if(!in_array($v['other_sku_detail_id'],$old_group_detail_ids)) unset($group_detail[$k]);

                    }

                }

                $new_group_detail=array_merge($group_detail,$delete_group_detail);

                $other_sku_detail_model= new GoodsOtherSkuDetailModel();

                $other_sku_detail_model->saveAll($new_group_detail);
            }


            Db::commit();
        }catch(\Exception $e){

            Db::rollback();

            return $this->error($e->getMessage());

        }

        return $this->success('操作成功');

    } 
    /**
     * 启用和关闭
    */
    public function saveStatus()
    {
        $other_sku_id = $this->request->param('other_sku_id');

        $status = $this->request->param('status','');

        $origin_type = $this->request->param('origin_type/d',1);

        if(null === $sku= GoodsOtherSkuModel::where(['other_sku_id'=>$other_sku_id,'shop_id'=>$this->request->shopId,'is_delete'=>0,'origin_type'=>$origin_type])->find())  return $this->error('规格不存在');

        if($status=='')  return $this->error('状态有误');

        $sku->status=$status;

        $sku->save();

        return $this->success('编辑成功');

    }
    /**
     * 删除/批量删除
     */
    public function del()
    {
        $other_sku_id = $this->request->param('other_sku_id');

        $origin_type = $this->request->param('origin_type/d',1);

        if(empty($other_sku_id))    return $this->error('规格不存在');

        if(null === $sku= GoodsOtherSkuModel::where(['is_delete'=>0,'shop_id'=>$this->request->shopId,'origin_type'=>$origin_type])->whereIn('other_sku_id',$other_sku_id)->find())  return $this->error('规格不存在');


        $sku->is_delete=1;

        $sku->save();

        return $this->success('删除成功');

    }
    /**
     * 获取单位列表
    */
    public function unitList(){
        $origin_type = $this->request->param('origin_type/d',1);
        $other_sku_ids = GoodsOtherSkuModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'type'=>3,'status'=>1,'origin_type'=>$origin_type])->column('other_sku_id');
        $detail = GoodsOtherSkuDetailModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'type'=>3])->whereIn('other_sku_id',$other_sku_ids)->field('other_sku_detail_id,group_detail_name')->select()->toArray();
        return $this->success($detail);
    }



}

